﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.Services;

public partial class ProductDetail : System.Web.UI.Page
{
    BLL.ProductBLL dataProduct = new BLL.ProductBLL();
    LabelBLL dataLabel = new LabelBLL();
    BrandCateBLL dataBrandCate = new BrandCateBLL();
    BrandBLL dataBrand = new BrandBLL();


    string idProduct = DateTime.Now.ToString();
    public string urlFb = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            string a = Request.Url.ToString(); ;
            urlFb = Request.RawUrl;
            if (Request.QueryString["Id"] != null)
            {
                idProduct = Request.QueryString["Id"].ToString();
            }
            LoadRptDetailProduct(idProduct);
            LoadRptProductOther();
        }
        Session["url_back"] = Request.RawUrl;
    }

    void LoadRptDetailProduct(string id)
    {
        DAL.Product product = dataProduct.GetById(id);
        System.Collections.Generic.List<Product> listProduct = new System.Collections.Generic.List<Product>();
        if (product != null)
        {
            listProduct.Add(product);
            DataTable dt = Myfunction.ConvertToDataTable(listProduct);

            rptDetailProduct.DataSource = dt;
            rptDetailProduct.DataBind();
        }
    }

    protected void rptDetailProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {

            //ProductBLL dataProduct = new ProductBLL();
            Label lbId = (Label)e.Item.FindControl("lbId");
            DAL.Product product = dataProduct.GetById(lbId.Text);

            product.Description = "http://giatueshop.com" + urlFb.ToString().Trim();
            dataProduct.Update(product);
            //Image
            Image img1 = (Image)e.Item.FindControl("img1");
            Image img2 = (Image)e.Item.FindControl("img2");
            Image img3 = (Image)e.Item.FindControl("img3");
            Image img4 = (Image)e.Item.FindControl("img4");
            img1.ImageUrl = "/Images/Product/big/" + product.Image_02;
            img2.ImageUrl = "/Images/Product/big/" + product.Image_03;
            img3.ImageUrl = "/Images/Product/big/" + product.Image_04;



            //cost
            Label lbCostOld = (Label)e.Item.FindControl("lbCostOld");
            Label lbCostNew = (Label)e.Item.FindControl("lbCostNew");
            if (product.Cost_Sale == null)
            {
                lbCostOld.Visible = false;
                string cost = string.Format("$ {0:0,0}", product.Cost);
                lbCostNew.Text = cost;
            }
            else
            {
                string cost = string.Format("$ {0:0,0}", product.Cost_Sale);
                lbCostNew.Text = cost;
            }

            //Label
            int LabelSpId = int.Parse(product.LabelId.ToString());
            LabelSP labelSP = dataLabel.GetById(LabelSpId);
            List<LabelSP> listLabel = new List<LabelSP>();
            listLabel.Add(labelSP);

            Repeater rptLabel = (Repeater)e.Item.FindControl("rptLabel");
            rptLabel.DataSource = Myfunction.ConvertToDataTable(listLabel);
            rptLabel.DataBind();

            //brand
            int idBrand = product.Brand;
            int IdbrandCate = dataBrand.GetById(idBrand).CateId;
            BrandCate brandCate = dataBrandCate.GetById(IdbrandCate);
            List<BrandCate> listBC = new List<BrandCate>();
            listBC.Add(brandCate);

            Repeater rptBrand = (Repeater)e.Item.FindControl("rptBrand");
            Repeater rptBrands = (Repeater)e.Item.FindControl("rptBrands");
            rptBrand.DataSource = Myfunction.ConvertToDataTable(listBC);
            rptBrand.DataBind();

            rptBrands.DataSource = Myfunction.ConvertToDataTable(listBC);
            rptBrands.DataBind();

            DropDownList dropSize = (DropDownList)e.Item.FindControl("dropSize");
            DropDownList dropSize2 = (DropDownList)e.Item.FindControl("dropSize2");
            LoadSize(idProduct, dropSize);
            LoadSize(idProduct, dropSize2);
            Button btnOneSize = (Button)e.Item.FindControl("btnOneSize");
            SizeBLL data = new SizeBLL();
            var listSize = data.GetByProductId(idProduct);
            if (listSize.Count <= 1) btnOneSize.Visible = true;
            else btnOneSize.Visible = false;

            Repeater rptColor = (Repeater)e.Item.FindControl("rptColor");
            LoadColor(idProduct, rptColor);

            string Name = product.Name;
            decimal Cost = 0;
            if (product.Cost_Sale == null)
                Cost = (decimal)product.Cost;
            else
                Cost = (decimal)product.Cost_Sale;
            string image = product.Image_01;

            TextBox txtQty = (TextBox)e.Item.FindControl("txtQty");
            DropDownList dropSizes = (DropDownList)e.Item.FindControl("dropSize");

            Button lbtCart = (Button)e.Item.FindControl("lbtCart");
            foreach (RepeaterItem item in rptColor.Items)
            {
                HtmlInputRadioButton htmlrdColor = (HtmlInputRadioButton)e.Item.FindControl("cbColor");

            }
            lbtCart.Click += new EventHandler(lbtCart_Click);
        }
        catch { Exception ex; }
    }

    void LoadRptProductOther()
    {
        string ProductId = Request.QueryString["Id"];
        int IdCate = dataProduct.GetById(ProductId).CategoryId;
        int top = 4;
        var listProduct = dataProduct.GetByTopOther(top, true, IdCate, ProductId);
        if (listProduct.Count == 0)
            productOther.Visible = false;
        else
        {
            DataTable tb = Myfunction.ConvertToDataTable(listProduct);
            rptListOther.DataSource = tb;
            rptListOther.DataBind();
        }
    }

    private void LoadSize(string IdProduct, DropDownList drop)
    {
        SizeBLL data = new SizeBLL();
        drop.DataSource = data.GetByProductId(IdProduct);
        drop.DataTextField = "Name";
        drop.DataValueField = "Id";
        drop.DataBind();
        ListItem itemRoot = new ListItem("Chọn size", "0");
        drop.Items.Insert(0, itemRoot);
    }

    private void LoadColor2(string IdProduct, DropDownList drop)
    {
        ColourBLL data = new ColourBLL();
        drop.DataSource = data.GetByProductId(IdProduct);
        drop.DataTextField = "Name";
        drop.DataValueField = "Id";
        drop.DataBind();
        ListItem itemRoot = new ListItem("Chọn color", "0");
        drop.Items.Insert(0, itemRoot);
    }

    private void LoadColor(string IdProduct, Repeater rptColor)
    {
        ColourBLL data = new ColourBLL();
        var list = data.GetByProductId(idProduct);
        DataTable tbColor = Myfunction.ConvertToDataTable(list);
        rptColor.DataSource = tbColor;
        rptColor.DataBind();
    }
    protected void rptDetailProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Repeater rptColor = (Repeater)e.Item.FindControl("rptColor");
        
    }
    protected void lbtCart_Click(object sender, EventArgs e)
    {

        foreach (RepeaterItem item in rptDetailProduct.Items)
        {
            ProductBLL data=new ProductBLL();
            ProductShop product = new ProductShop();
            
            Label lbId = (Label)item.FindControl("lbId");
            string Id = lbId.Text.Trim();
            DAL.Product p = data.GetById(Id);
            int Qty = 1;
            int size = 0;
            int idColor = 0;
            string nameColor = string.Empty;
            TextBox txtQty = (TextBox)item.FindControl("txtQty");
            Repeater rptColor = (Repeater)item.FindControl("rptColor");
            DropDownList dropSize = (DropDownList)item.FindControl("dropSize");
            foreach (RepeaterItem itemColor in rptColor.Items)
            {
                HtmlInputRadioButton htmlrdColor = (HtmlInputRadioButton)itemColor.FindControl("cbColor");
                 
                Label colorId = (Label)itemColor.FindControl("idColors");
                Label nameColors = (Label)itemColor.FindControl("nameColors");
                if (htmlrdColor.Checked == true)
                {
                    idColor = int.Parse(colorId.Text);
                    nameColor = nameColors.Text;
                }
            }
            Qty = int.Parse(txtQty.Text);
            size = int.Parse(dropSize.SelectedValue);
            product.Id = Id;
            product.Image = p.Image_01;
            product.Name = p.Name;
            product.Quantity = Qty;
            product.Colour = idColor;
            product.Size = size;
            if (p.Cost_Sale == null)
                product.Cost = (decimal)p.Cost;
            else product.Cost = (decimal)p.Cost_Sale;

            ((MyShopCart)Session["shop"]).Add(product);
            Response.Redirect(Request.RawUrl);
            ((MasterPage)this.Master).FindControl("ucMainNav1").Visible = false;
            Myfunction.ThongBao(ltThongBao, "Sản phẩm đã thêm vào giỏ hàng thành công !");
            

            
        }
    }

    protected void rptListOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            SizeBLL dataSize = new SizeBLL();
            string productId = Request.QueryString["Id"];

            var listSize = dataSize.GetByProductId(productId);
            DataTable tbSize = Myfunction.ConvertToDataTable(listSize);

            Repeater rptSize = (Repeater)e.Item.FindControl("rptSizeBuy");
            rptSize.DataSource = tbSize;
            rptSize.DataBind();
        }
        catch { Exception ex;
        }
    }
}